package org.example.myleet.rosalind.iprb;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Solution {
    //YY - homozygousDominant, Yy - heterozygous, yy - homozygousRecessive
    public String probabilityOfDominant(int homozygousDominant, int heterozygous, int homozygousRecessive) {
        int total = homozygousDominant + heterozygous + homozygousRecessive;
        if (total < 2) {
            //only one organism cannot mate
            return "0.0";
        }
        //YY x YY (100%)
        double homoDomXHomoDom = ((double) homozygousDominant / (double) total) * ((double) (homozygousDominant - 1) / (double) (total - 1));
        //YY x Yy (100%)
        double homoDomXHetero = ((double) homozygousDominant / (double) total) * ((double) heterozygous / (double) (total - 1));
        //YY x yy (100%)
        double homoDomXHomoRec = ((double) homozygousDominant / (double) total) * ((double) homozygousRecessive / (double) (total - 1));
        //Yy x YY (100%)
        double heteroXHomoDom = ((double) heterozygous / (double) total) * ((double) homozygousDominant / (double) (total - 1));
        //Yy x Yy (75%)
        double heteroXHetero = ((double) heterozygous / (double) total) * ((double) (heterozygous - 1) / (double) (total - 1));
        //Yy x yy (50%)
        double heteroXHomoRec = ((double) heterozygous / (double) total) * ((double) homozygousRecessive / (double) (total - 1));
        //yy x YY (100%)
        double homoRecXHomoDom = ((double) homozygousRecessive / (double) total) * ((double) homozygousDominant / (double) (total - 1));
        //yy x Yy (50%)
        double homoRecXHetero = ((double) homozygousRecessive / (double) total) * ((double) heterozygous / (double) (total - 1));
        //yy x yy (0%)
        double homoRecXHomoRec = ((double) homozygousRecessive / (double) total) * ((double) (homozygousRecessive - 1) / (double) (total - 1));
        double totalProbability = homoDomXHomoDom + homoDomXHetero + heteroXHomoDom + homoDomXHomoRec + homoRecXHomoDom + heteroXHetero + heteroXHomoRec + homoRecXHetero + homoRecXHomoRec;
        double sum = homoDomXHomoDom + homoDomXHetero + homoDomXHomoRec + heteroXHomoDom + heteroXHetero * 0.75 + heteroXHomoRec * 0.5 + homoRecXHomoDom + homoRecXHetero * 0.5;
        return BigDecimal.valueOf(sum).setScale(15, RoundingMode.HALF_UP).toPlainString();
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.probabilityOfDominant(2, 2, 2));
        System.out.println(solution.probabilityOfDominant(2, 3, 3));
        System.out.println(solution.probabilityOfDominant(16, 20, 18));
        System.out.println(solution.probabilityOfDominant(25, 19, 15));
        System.out.println(solution.probabilityOfDominant(15, 30, 27));
        System.out.println(solution.probabilityOfDominant(26, 22, 28));
    }
}
